// pages/home/pickHouseList/pickHouseList.js
import api from "../../../common/api.js";
import constant from "../../../common/constant.js";
import p from "../../../utils/platform.js";
import util from "../../../utils/util.js";

/** gio埋点 */
const _map = {
  NEWHOUSE: constant.XIN_FANG,
  RENT: constant.CHU_ZU,
  SALES: constant.ER_SHOU,
};

let option = "placard";
let titles = {
  placard: "海报房源选择",
  wxPublicity: "微信推广房源选择",
  pyqPublicity: "朋友圈推广房源选择",
};
let agentid;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabList: ["二手房", "出租房"],
    defaultTabIndex: 0,
    list: [],
    pyqVisible: false,
    detailContent: {},
    houseTypes: "",
    loading: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    option = options.option || option;

    wx.setNavigationBarTitle({
      title: titles[option],
    });

    getApp().globalData.loginPromise.then(({ sessionUser }) => {
      this.fetchHouse();

      agentid = sessionUser.id;
    });
  },

  tabnavIndexChange(e) {
    let index = e.detail.index;
    this.setData({
      defaultTabIndex: index,
    });

    this.fetchHouse();
  },

  fetchHouse() {
    this.setData({
      list: [],
      loading: true,
    });
    let com = this.selectComponent("#com-scroll-content");
    if (com) {
      com.fetchHouse(1, this.apiFetch.bind(this));
    }
  },

  friendsShareTabDialog() {
    this.setData({
      pyqVisible: false,
    });
  },

  apiFetch({ page, pageSize }) {
    this.setData({
      loading: true,
    });
    return new Promise((resolve, reject) => {
      let param = {
        page,
        pageSize,
        delegationType: this.data.defaultTabIndex == 0 ? "SALES" : "RENT",
        buildSize: [],
        area: [],
        price: [],
        floor: [],
        room: [],
        direction: [],
        decorate: [],
        buildType: [],
        distance: null,
        tag: [],
        order: null,
        sort: null,
        lat: wx.getStorageSync("latitude"),
        lng: wx.getStorageSync("longitude"),
        resblock_id: "",
        state: "AVAILABLE",
        resblock_name: "",
      };

      api.getMyHouseList({ houParamVO: JSON.stringify(param) }, (res) => {
        let data = res.data;
        data.handleData = this.handleData.bind(this);
        data.stupid = true;
        resolve(data);
      });
    });
  },
  handleData(rows) {
    this.setData({
      list: this.data.list.concat(rows),
      loading: false,
    });
  },

  handleClick(e) {
    let info = e.target.dataset.info;
    if (info) {
      console.log(info);

      switch (option) {
        // 海报分享
        case "placard":
          // console.log("海报分享");
          p.navigateTo({
            url: `/pages/visitors/tools/poster/visitorToolsPoster?houseid=${info.hou_del_id}`,
          });

          break;

        // 微信推广
        case "wxPublicity":
          this.goShareZhaofang(info);

          break;

        // 朋友圈推广
        case "pyqPublicity":
          p.showLoading({ title: "加载中" });

          let aldstat_op = wx.getStorageSync("aldstat_op");
          api.oldHouseInfo({ agentid, openid: aldstat_op }, () => {
            api.getHouseInfo({ houDelId: info.hou_del_id }, (res) => {
              this.setData({
                detailContent: res.data.content,
                pyqVisible: true,
              });

              p.hideLoading();
            });
          });

          break;
      }
    }
  },

  //跳转直接分享----到树懒找房小程序---带各种ID 和房源详情的相关数据
  goShareZhaofang({ hou_del_id, delegation_type }) {
    var allId = {};
    var loginUserInfo = getApp().globalData.loginUserInfo;
    // console.log({loginUserInfo});

    if (loginUserInfo) {
      allId.agentId = loginUserInfo.data.content.sessionUser.id;
      allId.storeId = loginUserInfo.data.content.sessionUser.extProps.storeId;
      allId.mkId = "EMP";
      allId.houDelId = hou_del_id;
    }

    api.shareHouseLog({
      sourceApp: "0",
      shareType: "1",
      delId: hou_del_id,
    });

    wx.gio("track", constant.HANDLE_SHARE, {
      [constant.ROOMTYPE_VAR]: _map[delegation_type],
      [constant.SHARETYPE_VAR]: constant.ZJ_SHARE,
      [constant.SHARESOURCE_VAR]: constant.DETAILS_SHARE,
    });

    var extraData = {
      allId: allId,
      type: "coupon",
    };
    extraData.allId.isTranspond = true;

    wx.navigateToMiniProgram({
      appId: "wx5adc5dd9870f6a26", //树懒找房极速版appid
      path: "pages/houseShare/RentalHouseDetailLite/RentalHouseDetailLite?jingjiId=" + JSON.stringify(extraData),
      extraData: extraData,
      envVersion: util.envVersion,
      success: (res) => {
        // 打开成功
        wx.gio("track", constant.SHARE_SUCCESS, {
          [constant.ROOMTYPE_VAR]: _map[delegation_type],
          [constant.SHARETYPE_VAR]: constant.ZJ_SHARE,
        });
      },
      fail(res) {
        //
      },
    });
  },

  handleMove() {
    p.navigateTo({
      url: "/pages/gatherHouse/scanAddHouse/scanAddHouse",
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

});
